/* 
   
   Girder 4.0 API Header
   Copyright (c) 2005 Promixis, LLC
   March 31 2005

   Upload your plugin at:
   http://www.promixis.com/uploads.php
	
   Request a plugin number from gsupport@promixis.com
   Support questions go to the forum
   http://www.promixis.com/phpBB2/index.php
   or gsupport@promixis.com

*/


#ifndef _INC_GIRDER_H
#define _INC_GIRDER_H

#include <windows.h>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
// return values of the gir_event function
#define retContinue       0   // Girder will continue working normally
#define retExplicitReturn 1   // Girder will stop after this command in the current Multigroup but continues on elsewhere.
#define retStopProcessing 2   // Girder will stop processing this event completely.

// The OSD settings for use with get_osd_settings
#define osdFGColor             1
#define osdFGDColor            2
#define osdBGColor             3
#define osdWidth               4
#define osdHeight              5
#define osdCaption             6
#define osdBorder              7
#define osdTransparent         8
#define osdFontsize            9
#define osdFontWeight          10
#define osdFontItalic          11
#define osdFontUnderline       12
#define osdFontStrikeout       13
#define osdFontWeight          10
#define osdFontItalic          11
#define osdFontUnderline       12
#define osdFontStrikeout       13
#define osdLeft                14
#define osdTop                 15
#define osdCenter              16
#define osdMonitor             17

// generic return values
#define GIR_TRUE               1 
#define GIR_FALSE              0
#define GIR_ASYNC_LEARN        2

#define GIRINFO_POWERBROADCAST 2
/* this passes the wparam and lparam directly from the WM_POWERBROADCAST, 
   to confirm the message pass GIR_TRUE.
*/

/* Currently not yet supported */
#define GIRINFO_WINLOGON       3

/* Wparam for GIRINFO_WINLOGON can be : */
#define WL_LOGON               0
#define WL_LOGOFF              1
#define WL_STARTUP             2
#define WL_SHUTDOWN            3
#define WL_STARTSCREENSAVER    4
#define WL_STOPSCREENSAVER     5
#define WL_STARTSHELL          6
#define WL_LOCK                7
#define WL_UNLOCK              8

/* This message is sent when the girder GUI window closes,
   you could close the plugin windows if needed */
#define GIRINFO_MAINWIN_CLOSE  4
/* This message is sent when the girder GUI window opens, wparam contains 
   the handle to the window*/
#define GIRINFO_MAINWIN_OPEN   5
/* This message is sent when the scripting engine changes state */
#define GIRINFO_SCRIPT_AFTER_STARTED	7
#define GIRINFO_SCRIPT_BEFORE_STOPPED	8
/* Sent when Windows sends out the WM_DISPLAY_CHANGED message, wparam and lparam are passed. */
#define GIRINFO_DISPLAY_CHANGED	9
/* Sent when Windows sends out the WM_DEVICECHANGED message. */
#define GIRINFO_DEVICE_CHANGE 10
// the command structure that girder passes to the plugin.
#define GIRINFO_PLUGIN_LOADED 11
// right after a plugin is loaded this is called. NOTE this is not called during startup loading of plugins.
// only if the loading happens by a script command or by clicking in the settings window.
#define GIRINFO_SCRIPT_AFTER_STARTED_A	12
#define GIRINFO_SCRIPT_BEFORE_STOPPED_A	13
#define GIRINFO_SCRIPT_LEVEL_START 14 // wparam hold LEVEL parameter and lparam holds luastate.


/**************************/
/* Girder 4.0 Definitions */
/**************************/

#define WM_OWNER_DELETE				WM_APP+4
#define WM_OWNER_CHANGE_REQUEST		WM_APP+5
#define WM_UPDATE_FIELDS			WM_APP+35
#define WM_CLOSE_FORM				WM_APP+36
#define WM_CHILDNODE_CHANGED		WM_APP+40
#define WM_NODE_CHANGED				WM_APP+41


#define WM_DUI_EVENT				WM_APP + 15


// stuff from shared.dll

typedef struct f_TFTreeNode
{
	void *         Data;
	struct f_TFTreeNode * Next;
	struct f_TFTreeNode * Prev;
	struct f_TFTreeNode * Child;
	struct f_TFTreeNode * Parent;

} TFTreeNode, *PFTreeNode;


typedef void  (WINAPI *t_FreeFunction) ( void * data, void * userdata);
typedef void * (WINAPI *t_DuplicateFunction) ( void * data, void * userdata);

typedef struct t_TFree 
{
	CRITICAL_SECTION CriticalSection;
	t_FreeFunction FreeFunction;
	t_DuplicateFunction DuplicateFunction;
	void *UserData;
	PFTreeNode RootNode;

} TFTree, *PFTree;

#define ftreeInsertBefore 0
#define ftreeInsertAfter 1
#define ftreeAddChildLast 2
#define ftreeNowhere 3


PFTree     WINAPI CreateFTree     ( t_FreeFunction free, t_DuplicateFunction duplicate, void * userdata);
void       WINAPI DeleteFTree     ( PFTree tree );
void       WINAPI ClearFTree      ( PFTree tree );
PFTreeNode WINAPI CreateFTreeNode ( PFTree tree );
void       WINAPI AddFTreeNode    ( PFTree tree, PFTreeNode destination, PFTreeNode node, int mode );
void       WINAPI DeleteFTreeNode ( PFTree tree, PFTreeNode node);
PFTreeNode WINAPI CopyFTreeNode   ( PFTree dtree, PFTree stree, PFTreeNode dest, PFTreeNode source, int mode);
void       WINAPI MoveFTreeNode   ( PFTree tree, PFTreeNode dest, PFTreeNode source, int mode);
void       WINAPI UnhookFTreeNode ( PFTree tree, PFTreeNode node);



// stuff from Girder.dll
int		   WINAPI SendEventEx			(const char * eventstring, void *payload, int len, int device, DWORD keymod);
void	   WINAPI GirderLogMessageEx    ( const char * Source, const char * Details, int Icon );

// Send Event contants
#define EVENT_MOD_ON		1
#define EVENT_MOD_OFF		2
#define EVENT_MOD_REPEAT	4
#define EVENT_MOD_ALL		7
#define EVENT_MOD_NONE		0

// Girder Log Message Ex Icon constants.
#define GLM_NO_ICON -1
#define GLM_INFORMATION_ICON 0
#define GLM_EXCLAMATION_ICON 1
#define GLM_ERROR_ICON 2
#define GLM_GOOD_ICON 3



// NodeType
#define ntBase			0
#define ntGroup			1
#define ntCommand		2
#define ntMultiGroup	3
#define ntEvent			4
#define	ntConditional	5
#define	ntMultiEvent	6
#define	ntUnkn			7
#define	ntFileRoot		8
#define	ntConfig		9

// MatchBy
#define mbWindowName	1
#define mbClassName		2
#define mbExeName		4
#define mbVersion		8
#define mbChildName		64
#define mbChildClass	128

// Stuff from target.dll
typedef struct  
{
	PCHAR		TargetName ;
	PCHAR		TargetClass;
	PCHAR		TargetExe;
	PCHAR		CTargetName;
	PCHAR		CTargetClass;
	BOOL		Submatch;
	BOOL		TopMost;
	BYTE		Matchby;
	BOOL		OneMatch;
	int			Matchnum;
	BOOL		MatchHidden;
	BOOL		UseVariables;
	DWORD		u_FileVersionMS;
	DWORD		u_FileVersionLS;
	DWORD		l_FileVersionMS;
	DWORD		l_FileVersionLS;

} TTarget, *PTarget;

typedef struct 
{
	
	BOOL		BreakExecution;
    BOOL		Jump;
    int			JumpID;
    GUID		JumpGUID;
    BOOL		Email;
    PCHAR		EmailTo;
    PCHAR		EmailFrom;
    PCHAR		EmailBody;
    BOOL		SysLog;
    BOOL		GirderLog;

} TError, *PError;


typedef struct
{
	PCHAR		sValue1;
	PCHAR		sValue2;
	PCHAR		sValue3;
	PCHAR		bValue1;
	PCHAR		bValue2;
	PCHAR		bValue3;
	PCHAR		iValue1;
	PCHAR		iValue2;
	PCHAR		iValue3;
	int			lValue1;
	GUID		FileGUID1;
	int			lValue2;
	GUID		FileGUID2;
	int			lValue3;
	GUID		FileGUID3;
	void *		Binary;
	UINT		Size;
} TAction, *PAction;

typedef struct
{
	int			Count;
	int			Begin;
	BOOL		Invert;
	int			Current;	
} TState, *PState;


typedef struct
{
	int			NodeType;
	PFTreeNode	Node;
	int			ID;
	BOOL		Enabled;
	PCHAR		Name;
	PCHAR		Comments;
	HWND		Owner;
	LPARAM		OwnerData;
} TBaseNode, *PBaseNode;

typedef struct
{
	int			NodeType;
	PFTreeNode	Node;
	int			ID;
	BOOL		Enabled;
	PCHAR		Name;
	PCHAR		Comments;
	HWND		Owner;
	LPARAM		OwnerData;
	TState		State;
	TAction		Action;
	
	BOOL		OSD;
	int			OSDTimeout;
	TTarget		Target;


	int			ActionType;
	int			ActionSubType;
	
	TError		Error;
} TCommand, *PCommand;



typedef struct
{
	int			NodeType;
	PFTreeNode	Node;
	int			ID;
	BOOL		Enabled;
	PCHAR		RegKey;
	PCHAR		Comments;
	HWND		Owner;
	LPARAM		OwnerData;	
	TAction		Action;
	int			Device;
	int			SubDev;
	void *		Plugin;
} TConfig, *PConfig;



typedef struct
{
	int			NodeType;
	PFTreeNode	Node;
	int			ID;
	BOOL		Enabled;
	PCHAR		Name;
	PCHAR		Comments;
	HWND		Owner;
	LPARAM		OwnerData;
	TTarget		Target;
	int			ActionType;
	int			ActionSubType;
	TAction		Action;
	TError		Error;
} TConditional, *PConditional;

typedef struct
{
  DWORD		Arrival;
  int		Devicel;
  PCHAR		EventString;
  BYTE*		Payload;
  DWORD		PayloadSize;
  int		Param1;
  int		Param2;
  int		Param3;
  HANDLE	Event;
  void *	internal1;
  void *	internal2;
  DWORD		Modifier;
} TEventElement, *PEventElement;


typedef struct
{
	int			NodeType;
	PFTreeNode	Node;
	int			ID;
	BOOL		Enabled;
	PCHAR		Name;
	PCHAR		Comments;
	HWND		Owner;
	LPARAM		OwnerData;	
	BOOL		Changed;
	GUID		FileGUID;
	PCHAR		FileName;
	int			TimeStamp;
	TError		Error;
} TFileRoot, *PFileRoot;

typedef struct 
{
	int			NodeType;
	PFTreeNode	Node;
	int			ID;
	BOOL		Enabled;
	PCHAR		Name;
	PCHAR		Comments;
	HWND		Owner;
	LPARAM		OwnerData;	
	BOOL		invert;
	PCHAR		EventString;
	int			Device;
	int			AntiRepeat;
	int			LastAntiRep;
	BOOL		Triggered;
	DWORD		KeyMod;
} TEvent, *PEvent;


void   WINAPI DeleteWindowTree(PFTree WinTree);
PFTree WINAPI GetWindowTree();
PFTree WINAPI GetProcessTree();
void   WINAPI DeleteProcessTree(PFTree ProcessTree);
PFTree WINAPI GetWindowMatches(PTarget target,PFTree WinTree,PFTree ProcessTree);
void   WINAPI DeleteWindowMatches(PFTree WindowMatchTree);
BOOL   WINAPI MatchWindow( HWND wnd, PTarget Target, PFTree ProcessTree);

#define TreeLockRead 1
#define TreeLockWrite 2
void   WINAPI LockTree( PFTree Tree, int Reason );
void   WINAPI UnlockTree( PFTree Tree, int Reason );
BOOL   WINAPI LockLua ( DWORD timeout);
void   WINAPI UnlockLua();
void   WINAPI SetTreePriority( PFTree Tree, int Priority);

void *     WINAPI Malloc          ( int size);
void       WINAPI SafeFree        ( void * p);
PCHAR      WINAPI GStrDup          ( const char * s);
void       WINAPI MemCopy		  ( void *dest, const void * source, int len);
void       WINAPI ClearMem        ( void *dest, int len, int fill);
PFTreeNode WINAPI FindNodeByGUID	( PFTree tree, PFTreeNode node, int ID, GUID fileguid );
void	   WINAPI GetFreeID			( PFTree tree, PFTreeNode node, int *ID);

void *	   WINAPI cAllocCommand	  ( void * UserData, int NodeType);
void		WINAPI TreeSetChanged	( PFTreeNode node, BOOL Changed );


// stuff from girder.dll

int        WINAPI UnloadPluginByID      ( int ID);
int        WINAPI LoadPluginByID        ( int ID);
int        WINAPI EnablePlugin          ( int ID);
int        WINAPI DisablePlugin         ( int ID);

PCHAR	   WINAPI ParseString			( PCHAR s );  
typedef int   (WINAPI *t_target_callback_ex2)       (HWND Wnd,  PCommand command,CRITICAL_SECTION *cs, void * UserData);
int		   WINAPI TargetEnum			( PCommand Command, CRITICAL_SECTION *cs, t_target_callback_ex2 Callback, void * UserData ) ;
int		   WINAPI GirderError			( int PluginID, PFTreeNode Node, int ErrorID, PCHAR ErrorText);

typedef BOOL   (WINAPI *t_event_handler)			(int HandleID, PEventElement event); 
int		   WINAPI AddEventHandler		( PCHAR Expression, int DevMin, int DevMax, t_event_handler EventHandler);
int		   WINAPI RemoveEventHandler	( int HandleID );

int		   WINAPI TriggerNodeEx			( int ID, GUID * FileGuid);
BOOL	   WINAPI lua_push_object		( void * L, const char * s);

void	   WINAPI LoadGMLFile			( PCHAR FileName );
void	   WINAPI LoadGMLFileEx			( PCHAR FileName, BOOL Force);
void	   WINAPI UnloadGMLFile			( GUID * G );
int		   WINAPI IsGMLLoaded			( GUID * G );


BOOL	   WINAPI IsLuaExiting			( ) ;
DWORD		WINAPI ReadEnvVar			( PCHAR name,  PCHAR buffer, DWORD size, BYTE xor);
BOOL		WINAPI SetEnvVar			( PCHAR name, PCHAR value, BYTE xor);
PCHAR		WINAPI PathExpand			( PCHAR Path);
int			WINAPI FindRootGUID			( PFTreeNode node, GUID * g );

int			WINAPI GetIdFromGuid		( GUID g );

void		WINAPI TargetToLua			( lua_State * L, PTarget Target);
BOOL		WINAPI LuaToTarget			( lua_State * L, int SP, PTarget Target);
HANDLE		WINAPI CreateFontFromString	( PCHAR FontString);
int			WINAPI GetFontColorFromString	( PCHAR FontString);


void	   WINAPI FreeAction		( PAction Action);


/* FILE IO */

BOOL	WINAPI XMLLoadDUITree			(PFTree Tree, PCHAR Filename);




/**************************/
/* Dynamic User Interface */
/**************************/

typedef struct {
	HWND hwnd;
	void * L;
	CRITICAL_SECTION CS;
	void * UserData;
} *pLuaRec;

#define GIRDERGROUPGUID "{F02CD0B5-B793-4013-87A9-344588CAD8D0}"
#define LEGACYGROUPGUID "{69C0161F-AD03-4547-9830-AAEA60C26795}"
#define OSGROUP			"{BB661954-1A7C-4BD7-A1F8-5EC74CE06361}"

void		WINAPI DUIWantUpdate		( int PluginID, int duCode ); // depreciated, potential for deadlocks, use DuiWantUpdateEx.
typedef void *   (WINAPI *t_gir_dui)       (PFTree DUITree, PFTreeNode DUINode, PBaseNode Base, int fDuiIndex, int Message, WPARAM wParam, LPARAM lParam, void * UserData);
void		WINAPI DUIWantUpdateEx		(	t_gir_dui gir_dui, int du, PCHAR PluginName); 

void *	   WINAPI AllocDUI		  ( void * UserData, int NodeType);
void	   WINAPI FreeDUI		  ( void * data, void * UserData);
void *	   WINAPI DuplicateDUI	  ( void * data, void * UserData);
void	   WINAPI CopyDUI		  ( void * Destination, void * Source, void * UserData);

#define duUnkn				0
#define duGroup				1
#define duItem				2
#define	duControl			3

#define duOnHookCommand			0
#define	duOnEvent				1
#define	duOnApply				2
#define duOnUnHookCommand		3
#define duOnGetDefaults			5
#define duOnShow				6
#define duEventLoadVars			7
#define duEventLoadPage			8
#define duOnHookConditional		9
#define duOnUnHookConditional	10
#define	duOnHookConfig			11
#define duOnUnHookConfig		12
#define duOnClose				13  // cleanup userdata value here.
//#define duOnGetPage				14 // duOnShow has this role now.
#define	duOnHandleChanged		15
#define duOnOpen				16	// return userdata values here.

#define duOnHookScriptConfig    17
#define duOnUnHookScriptConfig  18



#define	duEdit				1
#define	duLabel				2
#define	duTargetButton		3
#define	duGroupBox			4
#define	duComboBox			5
#define	duListBox			6
#define	duBrowseButton		7
#define	duCheckbox			8
#define	duEditComboBox		9
#define	duRadioGroup		10
#define	duSlider			11
#define	duCommandButton		12
#define	duLinkButton		13
#define	duMemo				14
#define	duVolumeButton		15
#define	duCodeEditor		16
#define	duCaptureButton		17
#define	duSpin				18
#define duDirectoryBrowse   19;
#define duHTMLLabel         20;
#define duImage             21;

#define	duFUndefined		0
#define	duFsValue1			1
#define	duFsValue2			2
#define	duFsValue3			3
#define	duFiValue1			4
#define	duFiValue2			5
#define	duFiValue3			6
#define	duFbValue1			7
#define	duFbValue2			8
#define	duFbValue3			9
#define	duFlValue1			10
#define	duFlValue2			11
#define	duFlValue3			12
#define	duFOSD				13
#define	duFOSDTimeout		14
#define	duLuaTable			15



#define	duAkLeft			1
#define	duAkTop				2
#define	duAkRight			4
#define	duAkBottom			8

typedef struct 
{
	int			NodeType;
	PCHAR		Name;
	PCHAR		Description;
	GUID		ID;
	int			ImageIndex;
	int			Show;
	
} TDUIGroup, *PDUIGroup;

typedef struct 
{
	int			NodeType;
	PCHAR		Name;
	PCHAR		Description;
	GUID		ID;
	int			ImageIndex;
	int			Show;
	PCHAR		LuaCode;
	BOOL		LuaEvent;
	int			ActionType;
	int			ActionSubType;
	
	void *		Plugin;	// plugin callback.

} TDUIItem, *PDUIItem;

typedef struct
{
	int			NodeType;
	PCHAR		Name;
	PCHAR		Description;
	GUID		ID;
	PCHAR		LuaCode;
	BOOL		LuaEvent;
	int			Field;
	void *		Plugin;	// plugin callback.
	int			ControlType;
	int			Width;
	int			Height;
	int			Left;
	int			Top;
	int			Anchors;
	PCHAR		Default;
	PCHAR		Values;
	PCHAR		LuaName;
	BOOL		Event;
	int			EventVal;
	PCHAR		Values2;

} TDUIControl, *PDUIControl;


typedef struct s_core_vars {
	DWORD		CoreThreadID;
	PFTree		CommandTree;
	PFTree		MappingTree;
	PFTree		PluginTree;
	HWND		MainWindow;	// note this might change during the lifetime of Girder a INFO Message will appear.
	void *		LuaState;	// note this might change during the lifetime of Girder a INFO message will appear.
	HANDLE		LuaMutex;	// note this might change during the lifetime of Girder a INFO message will appear.
	BOOL		GirderActive;
	PCHAR		ExePath;
	PFTree		CommandUITree;
	PFTree		ConditUITree;
	PFTree		ConfigUITree;
} sCoreVars, *pCoreVars;

typedef struct s_functions_3
{
   DWORD                size;
   pCoreVars			CoreVars;
} sFunctions3, *pFunctions3; 




#endif